#!/bin/bash
#Author:fuce
#Date:2023.8.7
#File:SystemToolbox.sh
#VersionNumber:v1.0.1
#Demand:Write system toolbox
#——————————————————————————————————
#script start
#<-----------------------询问继续使用工具箱函数定义
whether() {
  echo
  echo
  read -p "继续使用[y]，注销[n]:" play
  if [[ "$play" == "y" ]]; then
    clear
  else
    clear
    echo "Thank you for your use. Have a nice day!"
    exit

  fi
}
#菜单显示函数定义
ToolboxMenu() {
  echo -e "\033[5;34m =============This is a system detection toolbox==================\033[0m"
  echo -e "\033[33m ▌            1. 查看磁盘挂载信息                                  ▌ \033[0m"
  echo -e "\033[33m ▌            2. 查看内存信息                                      ▌ \033[0m"
  echo -e "\033[33m ▌            3. 查看CPU信息                                       ▌ \033[0m"
  echo -e "\033[33m ▌            4. 查看网络端口信息                                  ▌ \033[0m"
  echo -e "\033[33m ▌            5. 查看进程信息                                      ▌ \033[0m"
  echo -e "\033[33m ▌            6. 磁盘每秒进程下的I0读写数量                        ▌ \033[0m"
  echo -e "\033[33m ▌            7. 查看系统版本                                      ▌ \033[0m"
  echo -e "\033[33m ▌            8. 监控cpu、磁盘、内存使用率                         ▌ \033[0m"
  echo -e "\033[33m ▌            9. 根据端口查杀进程                                  ▌ \033[0m"
  echo -e "\033[33m ▌            10. 根据输入名称查询进程信息                         ▌ \033[0m"
  echo -e "\033[33m ▌            11. 解压文件                                         ▌ \033[0m"
  echo -e "\033[33m ▌            12. 压缩文件                                         ▌ \033[0m"
  echo -e "\033[33m ▌            13. 修改权限为777                                    ▌ \033[0m"
  echo -e "\033[33m ▌            14. 查找某个目录的文件                               ▌ \033[0m"
  echo -e "\033[33m ▌            bye. Exit Toolbox                                    ▌ \033[0m"
  echo -e "\033[33m ▌         当前时间:$(date "+%Y-%m-%d %H:%M:%S") 欢迎工程师!                ▌ \033[0m"
  echo -e "\033[5;34m ==================System detection toolbox=======================\033[0m"

}
#<----------------邀请用户输入需要检测的信息
while :; do
  ToolboxMenu
  read -p "请输入您的选择![1..14]" number
  case $number in
  #<-----------------------------------------------------------磁盘挂载信息
  1)
    echo -e "\033[5;34m  ============分区信息============\033[0m"
    df -hT
    whether
    ;;
    #<------------------------------------------------------内存使用情况
  2)
    echo -e "\033[5;34m ============内存使用============\033[0m"
    free -h
    n=1
    for i in {1..3}; do
      echo -e "\033[5;34m ============内存使用${n}============\033[0m"
      vmstat | awk '{if(NR==3){print "你还有空闲内存:" $4}}'
      let ++n
      sleep 1
    done
    whether
    ;;
    #<-------------------------------------------------------CPU利用率和负载
  3)
    v=1
    for i in {1..3}; do
      echo -e "\033[5;34m ============CPU利用率和负载${v}===========\033[0m"
      uptime
      vmstat | grep 2 | awk '{if (100-$15<=10){print 100-$15 "%","您的CPU很安全"}else{print 100-$15 "%","您的CPU即将满载请留意"}}'
      let ++v
      sleep 1
    done
    whether
    ;;
    #<---------------------------------------------------------------网络端口信息
  4)
    while :; do
      echo -e "\033[5;34m ============网络端口信息============\033[0m"
      read -p "请输入要查询的网络端口号:" Port
      netstat -anpt | head -2
      netstat -anpt | grep $Port
      read -p "你想检查端口吗?[y/n]" see
      if [[ ! $see == "y" ]]; then
        echo "感谢您的使用。祝你今天愉快!"
        exit
      else
        break
      fi
    done
    whether
    ;;
    #<----------------------------------------------------------------进程信息查询
  5)
    echo -e "\033[5;34m ============进程信息============\033[0m"
    read -p "请输入要查询的进程信息:" Process
    ps aux | grep -v grep | grep $Process
    whether
    ;;
    #<-------------------------------------------磁盘每秒进程下的IO读、写请求数量
  6)
    b=1
    for i in {1..3}; do
      echo -e "\033[5;34m ============Number of IO read / write requests issued by the process${b}============\033[0m"
      iostat | grep ^sd | awk '{print $1,$2}'
      let ++b
      sleep 1
    done
    whether
    ;;

    #<-------------------------------------------查看系统版本
  7)
    echo -e "\033[5;34m  ============系统版本============\033[0m"
    lsb_release -a
    whether
    ;;
    #<-------------------------------------------监控cpu、磁盘、内存使用率win模式
  8)
    echo -e "\033[5;34m  ============系统版本============\033[0m"
    #当前时间
    now_time=$(date '+%F %T')
    #获取cpu使用率
    cpuUsage=$(top -b -n1 | fgrep "Cpu" | awk '{print 100-$8,"%"}')
    #统计内存使用率
    mem_used_persent=$(free -m | fgrep "Mem" | awk '{printf "%d", ($3)/$2*100}')
    #获取磁盘使用率
    diskUsage=$(df -h | fgrep "/dev/vda1" | awk '{print $5}')
    echo "当前时间："${now_time}
    echo -e "CPU使用率：${cpuUsage}\n磁盘使用率：${diskUsage}\n内存使用率：${mem_used_persent}"
    whether
    ;;
    #<-------------------------------------------根据端口查杀进程
  9)
    echo -e "\033[5;34m  ============根据端口查杀进程============\033[0m"
    read -p "请输入要查询的端口信息:" Process
    port=$Process
    echo -e "\033[5;34m 你输入的端口为:${port} \033[0m"
    #查询端口是否有进程在
    port_status=$(netstat -nlt | grep ${port} | wc -l)

    time=$(date "+%Y-%m-%d %H:%M:%S")
    if [ $port_status -lt 1 ]; then
      echo -e "\033[5;34m[未占用] $time [端口 $port]\033[0m"
      #执行方法
      # funFGZS $pid 0
    else
      ps -ef | grep $port

      pid=$(lsof -i:$port | awk 'NR==2 {print $2}')
      echo -e "\033[5;34m[已占用][进程号 $pid] $time [端口 $port]\033[0m"
      #是否杀死该进程
      read -p "你想杀死端口吗?[y/n]" ss
      if [[ ! $ss == "y" ]]; then
        echo "感谢您的使用。祝你今天愉快!"
      else
        kill -9 $pid
        echo -e "\033[5;34m已经杀死!端口:${port} 进程号为:${pid} \033[0m"
      fi

    fi

    whether
    ;;
    #<-------------------------------------------根据输入名称查询进程信息
  10)
    echo -e "\033[5;34m  ============根据输入名称查询进程信息============\033[0m"
    read -p "请输入要查询的进程名字:" jcmz
    ps -ef | grep $jcmz
    whether
    ;;
    #<-------------------------------------------解压文件
  11)
    echo -e "\033[5;34m  ============解压文件============\033[0m"
    read -p "请输入要解压文件夹名字以及路径:" filepath

    read -p "你输入的文件路径为${filepath}? [y/n]" ss
    if [[ ! $ss == "y" ]]; then
      echo "请重新输入"
      clear
      continue
    else

      filename=$filepath
      extension="${filename##*.}"
      #echo "$extension"
      if [ $extension == "tar" ] || [ $extension == "gz" ]; then
        read -p "你输入解压后文件存放的路径? " fjcflj
        tar -xzvf $filename -C $fjcflj
        echo "${filename}文件已经解压${fjcflj}"
      elif [ $extension == "gzip" ]; then
        read -p "你输入解压后文件存放的路径? " fjcflj
        gzip -d -C $fjcflj $filename
        echo "${filename}文件已经解压${fjcflj}"
      elif [ $extension == "bzip2" ]; then
        read -p "你输入解压后文件存放的路径? " fjcflj
        bzip2 -dkf $filename -C $fjcflj
        echo "${filename}文件已经解压${fjcflj}"
      elif [ $extension == "zip" ]; then
        read -p "你输入解压后文件存放的路径? " fjcflj
        unzip $filename -d $fjcflj
        echo "${filename}文件已经解压${fjcflj}"
      else
        echo "找不到该后缀解压方式"
      fi

    fi
    whether
    ;;
    #<-------------------------------------------压缩文件
  12)
    echo -e "\033[5;34m  ============压缩文件============\033[0m"
    read -p "请输入需要压缩的文件或者文件夹:" filepath

    read -p "你输入的文件路径为${filepath}? [y/n]" ss
    if [[ ! $ss == "y" ]]; then
      echo "请重新输入"
      clear
      continue
    else
      #相对路径
      xdlk=$(dirname ${filepath})
      echo "相对路径:${xdlk}"
      read -p "你输入生成的压缩文件名不需要带后缀 " fjcflj
      #压缩名字带路径
      yslj="${xdlk}${fjcflj}.tar.gz"
      tar -czvf $yslj $filepath
      echo "tar -czvf $yslj -C $filepath"
      #echo "${filepath}文件已经压缩${yslj}"
    fi
    whether
    ;;
    #<-------------------------------------------授权777
  13)
    echo -e "\033[5;34m  ============授权777============\033[0m"
    echo "文件: chmod 777 file_name"
    echo "文件夹: chmod -R 777 files"
    echo -e "\033[5;34m  ============授权777============\033[0m"
    whether
    ;;
    #<-------------------------------------------查找文件
  14)
    echo -e "\033[5;34m  ============查找文件============\033[0m"
    read -p "请输入你需要查找的文件夹:" filepath
    read -p "请输入你需要查找的文件名称模糊即可:" mhfilename
    filename=($(find "$filepath" -name "*${mhfilename}*" -print0 | while IFS= read -r -d '' file; do echo "$file"; done))
    #循环打印
    for element in "${filename[@]}"; do
      echo $element
    done

    read -p "是否需要打开文件? [y/n]" sf
    if [[ ! $sf == "y" ]]; then
      clear
      continue
    else

      for element in "${filename[@]}"; do
        echo "打开文件命令为:tail -n 100 ${element}"
      done
    fi

    whether
    ;;
    #<---------------------------------退出
  bye)
    clear
    echo -e "感谢您的使用。祝你今天愉快!"
    exit
    ;;
    #<------------------没有匹配项重新显示菜单
  *)
    echo -e "重定向到菜单时出错! "
    ;;
  esac
done
#script end
